package cn.e_eap.esite.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import cn.e_eap.esite.bean.Employee;
import cn.e_eap.esite.manager.EmployeeManager;
import cn.e_eap.esite.web.SystemContext;

import com.opensymphony.xwork2.ModelDriven;

public class EmpAction extends BaseAction implements ModelDriven<Employee> {

    private EmployeeManager empManager;
    private List<Employee> datas;
    private Employee emp;
    private File file;
    private String fileFileName;

    public Employee getEmp() {
        return emp;
    }

    public void setEmp(Employee emp) {
        this.emp = emp;
    }
    private String ids;

    @Override
    public String execute() {
        setDatas(empManager.find(emp, this));
        return super.execute();
    }

    @Override
    public String input() {
        if(getMethod().equalsIgnoreCase("import")) {
        	return "import";
        }
    	if (emp.getId() > 0) {
            emp = empManager.findById(emp.getId());
        }
        
        return INPUT;
    }
    
    public String add() {
    	if(getMethod().equalsIgnoreCase("import")) {
    		return this.doImport();
    	}
    	return super.add();
    }
    
    protected String doImport(){
    	File saved = new File(FileAction.rootFolder + "/upload" , getFileFileName());
    	if(saved.exists()) {
    	}
    	return SystemContext.Action.ADD_SUCCESS;
    }

    @Override
    protected String doAdd() {
        empManager.add(emp);
        return super.doAdd();
    }

    @Override
    protected String doUpdate() {
        Employee emp1 = empManager.findById(this.emp.getId());
        if (emp1 == null) {
            throw new NullPointerException();
        }
        BeanUtils.copyProperties(this.emp, emp1);
        empManager.update(emp1);
        return super.doUpdate();
    }

    @Override
    public String delete() {
        empManager.delete(ids);
        return super.delete();
    }

    public String checkId() {
        Employee e = empManager.findByEmpId(emp.getEmpId());
        if (e != null) {
            this.addActionMessage("0");
        }
        return "check";
    }

    public EmployeeManager getEmpManager() {
        return empManager;
    }

    @Resource
    public void setEmpManager(EmployeeManager empManager) {
        this.empManager = empManager;
    }

    @Override
    public Employee getModel() {
        if (emp == null) {
            emp = new Employee();
        }
        return emp;
    }

    public List<Employee> getDatas() {
        return datas;
    }

    public void setDatas(List<Employee> datas) {
        this.datas = datas;
    }

    public String getIds() {
        return ids;
    }

    public void setIds(String ids) {
        this.ids = ids;
    }

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
}
